using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Collections.Generic;

namespace Silvery.Command
{
    public partial class CommandBox : UserControl, IGameCommandBox
	{        
		public CommandBox()
		{
            InitializeComponent();
            Commands = new List<IGameCommand>();
            Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            commandInput.KeyDown += new KeyEventHandler(commandInput_KeyDown);
            ANI_Hide.Completed += new EventHandler(ANI_Hide_Completed);
            Canvas.SetZIndex(this, 9999);
            DisplayHotKey = new GameControls.HotKey(Key.F9, ModifierKeys.Control);
            DisplayHotKey.HotKeyActivated += (hotkey) =>
            {
                if (this.Visibility == System.Windows.Visibility.Collapsed)
                    this.Show();
                else
                    this.Hide();
            };
            
            #region 默认指令集
            Commands.Add(new Cmd_Time());
            Commands.Add(new Cmd_Help(Commands));
            Commands.Add(new Cmd_Cls());
            Commands.Add(new Cmd_Logs());
            Commands.Add(new Cmd_EnableRedrawRegions());
            Commands.Add(new Cmd_ShowSceneBlocks());
            Commands.Add(new Cmd_HideSceneBlocks());
            #endregion
        }

        void ANI_Hide_Completed(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        void commandInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && commandInput.Text.Length > 0)
            {
                bool isExecute = false;
                var array = commandInput.Text.Split(' ');
                var s = array[0];
                var parameter = string.Empty;
                if (array.Length > 1)
                    foreach (var item in array)
                        parameter += item + " ";
                foreach (var item in this.Commands)
                {
                    if (item.Command == s)
                    {
                        item.Execute(this, parameter);                          
                        isExecute = true;
                        break;
                    }
                }
                AddSaveCommand(commandInput.Text);
                commandInput.Text = string.Empty;
                if (isExecute == false)
                    this.WriteLineToOutput("没有这个指令");                
            }
            switch (e.Key)
            {
                case Key.Up:
                    commandInput.Text = GetSaveCommand(inputindex <= 0 ? inputindex = 0 : inputindex -= 1);
                    break;
                case Key.Down:
                    commandInput.Text = GetSaveCommand(inputindex>=inputList.Count?inputindex = inputList.Count - 1: inputindex += 1);
                    break;
                case Key.Escape:
                    commandInput.Visibility = System.Windows.Visibility.Collapsed;
                    commandInput.Visibility = System.Windows.Visibility.Visible;
                    break;
            }
        }


        #region 指令保存
        List<string> inputList = new List<string>();
        int inputindex = 0;

        private void AddSaveCommand(string command)
        {
            inputList.Add(command);
            inputindex = inputList.Count;
        }
        public string GetSaveCommand(int i)
        {
            if (inputList.Count <= 0)
                return string.Empty;
            if (i < 0)
                i = 0;
            else
                if (i >= inputList.Count)
                    i = inputList.Count - 1;
            return inputList[i];
        }
        #endregion
        void Content_Resized(object sender, EventArgs e)
        {
            if (Application.Current.Host.Content.ActualHeight != double.NaN)
            {
                this.Width = Application.Current.Host.Content.ActualWidth;
                this.Height = Application.Current.Host.Content.ActualHeight;
                startKeyFrame.Value = endKeyFrame.Value = -Application.Current.Host.Content.ActualHeight / 2;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        public void Show()
        {
            this.Visibility = System.Windows.Visibility.Visible;
            ANI_Show.Begin();
        }
        /// <summary>
        /// 隐蔽
        /// </summary>
        public void Hide()
        {
            ANI_Hide.Begin();
        }
        /// <summary>
        /// 指令集
        /// </summary>
        public List<IGameCommand> Commands { get; private set; }
        /// <summary>
        /// 添加一行到输出界面
        /// </summary>
        /// <param name="content"></param>
        public void WriteLineToOutput(object content)
        {            
            var p = new Paragraph();
            if (content is Inline)
                p.Inlines.Add(content as Inline);
            else
                p.Inlines.Add(content.ToString());
            commandUot.Blocks.Add(p);
        }
        /// <summary>
        /// 输出控件,可能是TextBlock,TextBox,RichTextBox
        /// </summary>
        public FrameworkElement UotputControl
        {
            get { return commandUot; }
        }

        /// <summary>
        /// 显示使用的热键
        /// </summary>
        public GameControls.HotKey DisplayHotKey
        {
            get;
            private set;
        }
        /// <summary>
        /// 是否显示
        /// 比如我们期望在游戏中不会显示命令组件，设置为true，默认为flase
        /// </summary>
        public bool NotDisplay { get; set; }
    }
}